﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.DAO;
using CCH.Entity;

public partial class _30_3001_1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            sysfunctionDAO dao = new sysfunctionDAO();

            if (Request.QueryString["mode"] == "new")
            {
                this.lab_title.Text = dao.Get_titleName(3001) + " - 新增";
                this.calendar1._ADDate = DateTime.Now;

            }
            else
            {
                this.lab_title.Text = dao.Get_titleName(3001) + " - 修改";

                this.hidd_no.Value = Request.QueryString["ID"];

                int res_no = int.Parse(this.hidd_no.Value);

                research data = new researchDAO().Get_research(res_no);

                this.tbox_title.Text = data.res_title.ConvertToNewLine();
                this.tbox_abstract.Text = data.res_abstract.ConvertToNewLine();
                this.tbox_author.Text = data.res_author.ConvertToNewLine();
                this.tbox_public.Text = data.res_public.ConvertToNewLine();
                this.calendar1._ADDate = data.res_datetime.Value;

                if (!string.IsNullOrEmpty(data.file_path))
                {
                    this.HyperLink1.Text = data.file_name;
                    this.HyperLink1.NavigateUrl = "3001-2.ashx?res_no=" + data.res_no;
                }

                this.ddl_type.DataBind();
                this.ddl_type.Items.FindByValue(data.res_type.Value.ToString()).Selected = true;

            }



        }
    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        this.ddl_type.Focus();
    }

    protected void btn_ok_Click(object sender, EventArgs e)
    {
        if (this.check_Input())
        {
            string str = "";
            research data;
            researchDAO dao = new researchDAO();

            if (this.hidd_no.Value != "")
            {
                data = dao.Get_research(int.Parse(this.hidd_no.Value));
                str = "修改完成!";
            }
            else
            {
                data = new research();
                str = "新增完成!";
                data.state = "1";
            }

            data.res_type = int.Parse(this.ddl_type.SelectedValue);
            data.res_title = this.tbox_title.Text.ConvertToBR();
            data.res_abstract = this.tbox_abstract.Text.ConvertToBR();
            data.res_author = this.tbox_author.Text.ConvertToBR();
            data.res_public = this.tbox_public.Text.ConvertToBR();
            data.res_datetime = this.calendar1._ADDate;
            data.createuid = new SessionObject().SessionUserID;
            data.createtime = DateTime.Now;

            if (this.FileUpload1.HasFile)
            {
                //存檔目錄
                string root = System.Configuration.ConfigurationManager.AppSettings["FileUpload"];

                //副檔名
                string exi = this.FileUpload1.FileName.Split('.')[1].ToLower();
                //檔名
                string filename = Guid.NewGuid().ToString("N") + "." + exi;
                //上傳
                this.FileUpload1.SaveAs(root + filename);

                data.file_name = this.FileUpload1.FileName;
                data.file_type = exi;
                data.file_path = root + filename;//連同新檔名
            }

            if (this.hidd_no.Value == "")
            {
                dao.Add_research(data);
            }

            dao.Update();


            //更新父代網頁
            JsUtil.UpdateParentJs(this, str);
        }
    }

    private bool check_Input()
    {
        if (string.IsNullOrEmpty(this.tbox_title.Text))
        {
            JsUtil.AlertJs(this, "請輸入研究主題!");
            return false;
        }

        if (string.IsNullOrEmpty(this.tbox_abstract.Text))
        {
            JsUtil.AlertJs(this, "請輸入研究摘要!");
            return false;
        }

        if (string.IsNullOrEmpty(this.tbox_author.Text))
        {
            JsUtil.AlertJs(this, "請輸入作者!");
            return false;
        }

        if (string.IsNullOrEmpty(this.tbox_public.Text))
        {
            JsUtil.AlertJs(this, "請輸入出處!");
            return false;
        }

        if (!this.calendar1.CheckDateTime())
        {
            JsUtil.AlertJs(this, "請輸入正確日期!");
            return false;
        }

        if (this.hidd_no.Value == "" && !this.FileUpload1.HasFile)
        {
            JsUtil.AlertJs(this, "請上傳附檔!");
            return false;
        }

        return true;
    }


}